package com.yumi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yumi.common.Result;
import com.yumi.entity.TrainingRecord;
import com.yumi.service.TrainingRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 培训记录控制器
 */
@RestController
@RequestMapping("/api/training/records")
@Slf4j
@Tag(name = "培训记录接口", description = "培训记录管理相关接口")
public class TrainingRecordController {

    @Autowired
    private TrainingRecordService trainingRecordService;

    /**
     * 添加培训记录
     * @param trainingRecord 培训记录信息
     * @return 添加结果
     */
    @PostMapping
    @Operation(summary = "添加培训记录", description = "添加单条培训记录信息")
    public Result<Boolean> addTrainingRecord(@RequestBody @Valid TrainingRecord trainingRecord) {
        log.info("添加培训记录：{}", trainingRecord);
        boolean result = trainingRecordService.addTrainingRecord(trainingRecord);
        return Result.success(result);
    }

    /**
     * 批量添加培训记录
     * @param trainingRecords 培训记录列表
     * @return 添加结果
     */
    @PostMapping("/batch")
    @Operation(summary = "批量添加培训记录", description = "批量添加多条培训记录信息")
    public Result<Boolean> batchAddTrainingRecords(@RequestBody @Valid List<TrainingRecord> trainingRecords) {
        log.info("批量添加培训记录，数量：{}", trainingRecords.size());
        boolean result = trainingRecordService.batchAddTrainingRecords(trainingRecords);
        return Result.success(result);
    }

    /**
     * 修改培训记录
     * @param id 记录ID
     * @param trainingRecord 培训记录信息
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "修改培训记录", description = "根据ID修改培训记录信息")
    public Result<Boolean> updateTrainingRecord(
            @PathVariable("id") @Parameter(description = "记录ID") Long id,
            @RequestBody @Valid TrainingRecord trainingRecord) {
        log.info("修改培训记录，ID：{}，数据：{}", id, trainingRecord);
        trainingRecord.setId(id);
        boolean result = trainingRecordService.updateTrainingRecord(trainingRecord);
        return Result.success(result);
    }

    /**
     * 删除培训记录
     * @param id 记录ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除培训记录", description = "根据ID删除培训记录")
    public Result<Boolean> deleteTrainingRecord(
            @PathVariable("id") @Parameter(description = "记录ID") Long id) {
        log.info("删除培训记录，ID：{}", id);
        boolean result = trainingRecordService.deleteTrainingRecord(id);
        return Result.success(result);
    }

    /**
     * 根据ID查询培训记录
     * @param id 记录ID
     * @return 培训记录信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询培训记录详情", description = "根据ID查询培训记录详细信息")
    public Result<TrainingRecord> getTrainingRecordById(
            @PathVariable("id") @Parameter(description = "记录ID") Long id) {
        log.info("查询培训记录详情，ID：{}", id);
        TrainingRecord trainingRecord = trainingRecordService.getTrainingRecordById(id);
        return Result.success(trainingRecord);
    }

    /**
     * 分页查询培训记录
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param trainingId 培训计划ID
     * @param employeeId 员工ID
     * @return 培训记录分页结果
     */
    @GetMapping
    @Operation(summary = "分页查询培训记录", description = "根据条件分页查询培训记录")
    public Result<Page<TrainingRecord>> getTrainingRecordPage(
            @RequestParam(value = "pageNum", defaultValue = "1") @Parameter(description = "页码") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") @Parameter(description = "每页大小") Integer pageSize,
            @RequestParam(value = "trainingId", required = false) @Parameter(description = "培训计划ID") Long trainingId,
            @RequestParam(value = "employeeId", required = false) @Parameter(description = "员工ID") Long employeeId) {
        log.info("分页查询培训记录，页码：{}，每页大小：{}，培训ID：{}，员工ID：{}", pageNum, pageSize, trainingId, employeeId);
        Page<TrainingRecord> page = new Page<>(pageNum, pageSize);
        Page<TrainingRecord> result = trainingRecordService.getTrainingRecordPage(page, trainingId, employeeId);
        return Result.success(result);
    }
} 